package cn.xxstudy.blog.entity;

import org.apache.tomcat.util.buf.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

/**
 * 作者：xinxi on 2020/11/24 22:38
 * 包名：cn.xxstudy.blog.entity
 * TODO：博客表
 */
@Entity
@Table(name = "personal_blog")
public class Blog {
    @Id
    @GeneratedValue
    private Long id;

    private String title;
    //由于content内容多 因此使用懒加载
    //@Log 表示持久化为Blob(字节类型)、Clob(长字符串类型)
    @Basic(fetch = FetchType.LAZY)
    @Lob
    private String content;
    private String flag = "原创";
    private String imgUrl;

    //浏览次数
    private int lookCount = 0;

    //是否勾选推荐
    private boolean recommendChecked = false;

    //是否勾选赞赏
    private boolean appreciateChecked = false;

    //是否勾选版权
    private boolean copyrightChecked = false;

    //是否勾选评论
    private boolean commentChecked = false;

    //是否发布
    private boolean published = false;

    //创建时间
    @Temporal(TemporalType.TIMESTAMP)
    private Date createDateTime = new Date();

    //修改时间,默认为创建时间
    @Temporal(TemporalType.TIMESTAMP)
    private Date updateDateTime = new Date();

    @ManyToOne
    private Category category;

    @ManyToMany(cascade = {CascadeType.PERSIST})
    private List<Tags> tags = new ArrayList<>();

    @ManyToOne
    private User user;

    @Transient
    private String tagIds;

    private String description;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getFlag() {
        return flag;
    }

    public void setFlag(String flag) {
        this.flag = flag;
    }

    public String getImgUrl() {
        return imgUrl;
    }

    public void setImgUrl(String imgUrl) {
        this.imgUrl = imgUrl;
    }

    public int getLookCount() {
        return lookCount;
    }

    public void setLookCount(int lookCount) {
        this.lookCount = lookCount;
    }

    public boolean isAppreciateChecked() {
        return appreciateChecked;
    }

    public void setAppreciateChecked(boolean appreciateChecked) {
        this.appreciateChecked = appreciateChecked;
    }

    public boolean isCopyrightChecked() {
        return copyrightChecked;
    }

    public void setCopyrightChecked(boolean copyrightChecked) {
        this.copyrightChecked = copyrightChecked;
    }

    public boolean isCommentChecked() {
        return commentChecked;
    }

    public void setCommentChecked(boolean commentChecked) {
        this.commentChecked = commentChecked;
    }

    public boolean isPublished() {
        return published;
    }

    public void setPublished(boolean published) {
        this.published = published;
    }

    public Date getCreateDateTime() {
        return createDateTime;
    }

    public void setCreateDateTime(Date createDateTime) {
        this.createDateTime = createDateTime;
    }

    public Date getUpdateDateTime() {
        return updateDateTime;
    }

    public void setUpdateDateTime(Date updateDateTime) {
        this.updateDateTime = updateDateTime;
    }

    public Category getCategory() {
        return category;
    }

    public void setCategory(Category category) {
        this.category = category;
    }

    public List<Tags> getTags() {
        return tags;
    }

    public void setTags(List<Tags> tags) {
        this.tags = tags;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public boolean isRecommendChecked() {
        return recommendChecked;
    }

    public void setRecommendChecked(boolean recommendChecked) {
        this.recommendChecked = recommendChecked;
    }

    public String getTagIds() {
        return tagIds;
    }

    public void setTagIds(String tagIds) {
        this.tagIds = tagIds;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public void initTagsFormat() {
        List<String> collect = tags.stream().map(it -> it.getId().toString()).collect(Collectors.toList());
        this.tagIds = StringUtils.join(collect, ',');
    }

    @Override
    public String toString() {
        return "Blog{" +
                "id=" + id +
                ", title='" + title + '\'' +
                ", content='" + content + '\'' +
                ", flag='" + flag + '\'' +
                ", imgUrl='" + imgUrl + '\'' +
                ", lookCount=" + lookCount +
                ", recommendChecked=" + recommendChecked +
                ", appreciateChecked=" + appreciateChecked +
                ", copyrightChecked=" + copyrightChecked +
                ", commentChecked=" + commentChecked +
                ", published=" + published +
                ", createDateTime=" + createDateTime +
                ", updateDateTime=" + updateDateTime +
                ", category=" + category +
                ", tags=" + tags +
                ", user=" + user +
                ", tagIds='" + tagIds + '\'' +
                ", description='" + description + '\'' +
                '}';
    }
}
